using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using THSLib;

namespace THSERP.BaseProvider
{
    public partial class guiReport : THSERPScreen
    {
        public guiReport()
        {
            InitializeComponent();            
        }

        private void guiReport_Load(object sender, EventArgs e)
        {
            InitReports();            
        }

        public String ReportName
        {
            get
            {
                return fld_lkeReport.Text;
            }
        }

        private void InitReports()
        {                       
            PRReportsController objPRReportsController = new PRReportsController();
            DataSet dsReports=objPRReportsController.GetPRReportsByModuleName(Module.Name);
            fld_lkeReport.Properties.DataSource = dsReports.Tables[0];
            fld_lkeReport.Properties.DisplayMember = "PRReportName";
            fld_lkeReport.Properties.ValueMember = "PRReportID";
            
            fld_lkeReport.ItemIndex = 0;                      
        }      

        private void fld_btnPrint_Click(object sender, EventArgs e)
        {
            
                String strReportPath = Application.StartupPath + ConfigurationManager.AppSettings["ReportPath"];
                if (!File.Exists(strReportPath + "\\" + Module.Name + "\\" + ReportName + ".rpt"))
                {
                    MessageBox.Show("Formular ist nicht vorhanden.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                Cursor.Current = Cursors.WaitCursor;
                Hashtable paramValues = new Hashtable();

                DataTable tblSearchResults = GetDataTableFromGridSearchResults();
                ReportLoader _reportLoader = new ReportLoader(ReportName, Module.Name);


                

                
                String strparamObjectNumberName = String.Format("param{0}Number", Module.Name);
                String strparamRemarkName = "paramRemark";
                
                paramValues.Add(strparamRemarkName, fld_medRemark.Text);
    
                _reportLoader.DisplayReport(paramValues);
                
                Cursor.Current = Cursors.Default;
            
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region Get Data From Search Results functions
        private DataTable GetDataTableFromGridSearchResults()
        {
            DataTable tblSearchResults = new DataTable();
            STFieldsController objSTFieldsController = new STFieldsController();
            String strTableName=THSUtil.GetTableNameFromBusinessObject(((BaseModuleERP)Module).CurrentModuleEntity.MainObject);
            STFieldsInfo objSTFieldsInfo = objSTFieldsController.GetFirstFieldByModuleIDAndUserGroupIDAndFieldDataSourceAndFieldTypeAndFieldTag(
                                                                ((BaseModuleERP)Module).ModuleID, THSApp.CurrentUserGroupInfo.ADUserGroupID,
                                                                strTableName, "THSGridControl", THSScreen.SearchResultControl);
                                                                
            string strSearchResultsControlName = objSTFieldsInfo.STFieldName;
            if (Module.Controls[strSearchResultsControlName] != null)
            {
                DevExpress.XtraGrid.GridControl _GridControl = (DevExpress.XtraGrid.GridControl)Module.Controls[strSearchResultsControlName];
                DevExpress.XtraGrid.Views.Grid.GridView gridView = _GridControl.Views[0] as DevExpress.XtraGrid.Views.Grid.GridView;
                DataTable dt = new DataTable();
                
                DataSet dsColumns = THSDbUtil.GetTableColumns(strTableName);
                foreach (DataRow rowColumn in dsColumns.Tables[0].Rows)
                {
                    String strColumnName = rowColumn["COLUMN_NAME"].ToString();
                    DataColumn column = new DataColumn();
                    column.DataType = THSUtil.GetColumnDataType(strTableName, strColumnName);
                    column.ColumnName = strColumnName;
                    column.Caption = strColumnName;
                    column.ReadOnly = true;
                    tblSearchResults.Columns.Add(column);
                }

                //for (int i = 0; i < gridView.Columns.Count; i++)
                //{
                //    DataColumn column = new DataColumn();
                //    column.DataType = gridView.Columns[i].ColumnType;
                //    column.ColumnName = gridView.Columns[i].FieldName;
                //    column.Caption = gridView.Columns[i].Caption;
                //    column.ReadOnly = true;
                //    tblSearchResults.Columns.Add(column);
                //}

                for (int i = 0; i < gridView.RowCount; i++)
                {
                    DataRow row = gridView.GetDataRow(i);
                    object[] arrobj = row.ItemArray;
                    tblSearchResults.Rows.Add(arrobj);
                }
            }
            return tblSearchResults;

        }
        #endregion

        private void fld_btnMultiPrint_Click(object sender, EventArgs e)
        {
            String strReportPath = Application.StartupPath + ConfigurationManager.AppSettings["ReportPath"];
            if (!File.Exists(strReportPath + "\\" + Module.Name + "\\" + ReportName + ".rpt"))
            {
                MessageBox.Show("Formular ist nicht vorhanden.", "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
            Hashtable paramValues = new Hashtable();
            paramValues.Add("paramRemark", fld_medRemark.Text);

            DataTable tblSearchResults = GetDataTableFromGridSearchResults();
            ReportLoader _reportLoader = new ReportLoader(ReportName, Module.Name);

            _reportLoader.DisplayReportFromDataTableWithParameters(paramValues, tblSearchResults, true);


            Cursor.Current = Cursors.Default;
        }


    }
}